package com.enlorenz.common.geo.map.arcgis;

import com.enlorenz.common.geo.map.base.TileWrapper;
import com.enlorenz.common.geo.map.bing.BingTileFileParser;
import com.enlorenz.util.CodeUtil;

/**
 * 名称转换
 * @author jueyue
 *
 */
public class ArcgisNameTransform {
	/**
	 * 返回参数GoogleTileWrapper
	 * @param paths
	 * @return
	 */
	public static TileWrapper google2Arcgis(TileWrapper googleTileWrapper){
		String level = googleTileWrapper.getZoom();
		String row = googleTileWrapper.getYtile();
		String col = googleTileWrapper.getXtile();
		
		TileWrapper outputWrapper=new TileWrapper();
		outputWrapper.setXtile(getRow(row));
		outputWrapper.setYtile(getCol(col));
		outputWrapper.setZoom(getLevel(level));
		
		return outputWrapper;
	}
	
	public static TileWrapper arcgis2Google(TileWrapper googleTileWrapper){
		String level = googleTileWrapper.getZoom();
		String row = googleTileWrapper.getYtile();
		String col = googleTileWrapper.getXtile();
		
		TileWrapper outputWrapper=new TileWrapper();
		outputWrapper.setXtile(getRowGoogle(row));
		outputWrapper.setYtile(getColGoogle(col));
		outputWrapper.setZoom(getLevelGoogle(level));
		
		return outputWrapper;
	}
	
	public static void main(String[] args) {
		TileWrapper googleTileWrapper=new TileWrapper();
		googleTileWrapper.setXtile("6825");
		googleTileWrapper.setYtile("2727");
		googleTileWrapper.setZoom("13");
		
		TileWrapper ou=google2Arcgis(googleTileWrapper);
		ou.setXtile("R00000abb");
		ou.setYtile("C00001738");
		ou.setZoom("L13");
		
		TileWrapper ou2=arcgis2Google(ou);
		
		String code=BingTileFileParser.tileXYToQuadKey(Integer.valueOf(ou2.getXtile()),
				Integer.valueOf(ou2.getYtile()),
				Integer.valueOf(ou2.getZoom()));
		System.out.println(code);
		System.out.println(googleTileWrapper);
		System.out.println(ou);
		System.out.println(ou2);
	}
	
	/**
	 * 
	 * 
	 * 获取行名
	 * @param row
	 * @return
	 */
	private static String getRowGoogle(String row) {
		row =row.substring(1);
		return String.valueOf(CodeUtil.byteHex2TenStr(row));
	}
	
	/**
	 * 获取列名
	 * @param col
	 * @return
	 */
	private static String getColGoogle(String col) {
		col = col.substring(1);
		return String.valueOf(CodeUtil.byteHex2TenStr(col));
	}
	
	/**
	 * google转换为arcgis的Level
	 * @param level
	 * @return
	 */
	private static String getLevelGoogle(String level) {
		level = level.substring(1);
		return String.valueOf(Integer.valueOf(level));
	}
	
	
	/**
	 * 获取行名
	 * @param row
	 * @return
	 */
	private static String getRow(String row) {
		row = Integer.toHexString(Integer.valueOf(row));
		row = "R"+getZeros(row) +row;
		return row;
	}
	
	/**
	 * 获取列名
	 * @param col
	 * @return
	 */
	private static String getCol(String col) {
		col = Integer.toHexString(Integer.valueOf(col));
		col = "C"+getZeros(col) +col;
		return col;
	}
	
	/**
	 * google转换为arcgis的Level
	 * @param level
	 * @return
	 */
	private static String getLevel(String level) {
		if(level.length()>1){
			level = "L"+level;
		}else{
			level = "L0"+level;
		}
		return level;
	}
	/**
	 * 获取不够的零
	 * @param row
	 * @return
	 */
	private static String getZeros(String row) {
		String result ="";
		for(int i = row.length();i<8;i++){
			result+="0";
		}
		return result;
	}

}
